package org.bdware.irs.backend.servicesImpl;

import org.apache.log4j.Logger;
import org.bdware.irs.backend.dto.Organization;
import org.bdware.irs.server.http.GrsHandler;
import org.bdware.irs.server.http.GrsMain;
import org.springframework.stereotype.Service;

import java.util.List;

//@Service
public class GrsServiceForHttp {
    GrsHandler grsHandler;
    BDGrsStorageRocksDBImpl prefixAndSuffixStorage;
    static Logger logger = Logger.getLogger(GrsServiceForHttp.class);

    public GrsServiceForHttp() throws InterruptedException {
        int count = 0;
        while (GrsMain.getGrsHandler() == null) {
            logger.info("wait for DOIP Service initialization");
            Thread.sleep(1000);
            if (count++ > 5) break;
        }
        if (GrsMain.getGrsHandler() == null) {
            logger.error("GRS not initialized.");
        }

        logger.info("====init grs service===");
        grsHandler = GrsMain.getGrsHandler();
        prefixAndSuffixStorage = GrsMain.getStore();
    }

    public Organization getOrganization(String doi) {
        return prefixAndSuffixStorage.getOrganization(doi);
    }

    public void saveOrganization(Organization organization) {
        prefixAndSuffixStorage.saveOrganization(organization);
    }

    public boolean updateOrganization(Organization organization) {
        return prefixAndSuffixStorage.updateOrganization(organization);
    }

    public List<Organization> getAllOrganization() {
        return prefixAndSuffixStorage.getAllOrganization();
    }


}
